package com.yiren.dbaa.module.practice_set.site.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lljjcoder.Interface.OnCustomCityPickerItemClickListener;
import com.lljjcoder.bean.CustomCityData;
import com.lljjcoder.style.citycustome.CustomCityPicker;
import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.module.practice_set.site.bean.CityBean;
import com.yiren.dbaa.module.practice_set.site.contract.AddSiteRuleContract;
import com.yiren.dbaa.module.practice_set.site.presenter.AddSiteRulePresenter;
import com.yiren.dbaa.module.practice_set.site.view.adapter.CitySelectorView;
import com.yiren.dbaa.module.practice_set.site.view.adapter.SiteRuleAdapter;
import com.yiren.dbaa.view.dialog.LoadDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/9/22 8:52
 * @description: 新建规则页面  选择器
 */
public class AddRuleActivity extends BaseActivity implements AddSiteRuleContract.AddSiteRuleView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.et_rule_creator)
    EditText etRuleCreator;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.lv_share_site)
    ListView lvShareSite;

    private CustomCityPicker customCityPicker;
    private AddSiteRuleContract.Presenter addSiteRulePresenter;
    private SiteRuleAdapter siteRuleAdapter;
    private String cityID = "";
    private CityBean cityBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_rule);
        ButterKnife.bind(this);
        tvTitle.setText("新建考场规则");
        init();
    }

    private void init() {
        cityBean = new CityBean();
        addSiteRulePresenter = new AddSiteRulePresenter(this);
        addSiteRulePresenter.loadProvinceCity(this);
    }

    @OnClick({R.id.iv_back, R.id.tv_city, R.id.tv_select, R.id.ll_new_rule})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_city:
                if (customCityPicker != null) {
                    customCityPicker.showCityPicker();
                }
                break;
            case R.id.tv_select:
                addSiteRulePresenter.loadShareSiteRule(this, cityID, etRuleCreator.getText().toString());
                break;
            case R.id.ll_new_rule:
                startActivity(new Intent(this, AddRuleInfoActivity.class));
                break;
        }
    }

    @Override
    public void showShareSiteRuleList(List<SiteEntity> shareSites) {
        if (shareSites.size() == 0) {
            llNoData.setVisibility(View.VISIBLE);
            lvShareSite.setVisibility(View.GONE);
            tvTips.setText("当前暂无共享考规数据！");
        } else {
            llNoData.setVisibility(View.GONE);
            lvShareSite.setVisibility(View.VISIBLE);
            siteRuleAdapter = new SiteRuleAdapter(this, shareSites, 1);
            lvShareSite.setAdapter(siteRuleAdapter);
            //选中新建单机事件
            siteRuleAdapter.setOnManageClickListener(position -> {
                Intent intent = new Intent(this, AddRuleInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("city", cityBean);
                bundle.putSerializable("site", shareSites.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            });
        }
    }

    @Override
    public void showProvinceCity(List<com.yiren.dbaa.module.practice_set.site.bean.ProvinceBean> provinceList) {
        CitySelectorView.setCityData(provinceList);
        customCityPicker = CitySelectorView.createView(this);
        LoadDialog.dismiss(this);
        customCityPicker.setOnCustomCityPickerItemClickListener(new OnCustomCityPickerItemClickListener() {
            @Override
            public void onSelected(CustomCityData province, CustomCityData city, CustomCityData district) {
                super.onSelected(province, city, district);
                cityBean.setId(city.getId());
                cityBean.setName(city.getName());
                tvCity.setText(city.toString());
                cityID = city.getId();
            }
        });
        //加载当前城市考规数据
        String city = GpsInfoBean.getInstance().getCity();
        String pro = GpsInfoBean.getInstance().getProvince();
        if (!StringUtil.isEmpty(city)) {
            tvCity.setText(city);
            for (int i = 0; i < CitySelectorView.mProvinceListData.size(); i++) {
                if (CitySelectorView.mProvinceListData.get(i).getName().equals(pro)) {
                    for (int j = 0; j < CitySelectorView.mProvinceListData.get(i).getList().size(); j++) {
                        if (CitySelectorView.mProvinceListData.get(i).getList().get(j).getName().equals(city)) {
                            GpsInfoBean.getInstance().setCityId(CitySelectorView.mProvinceListData.get(i).getList().get(j).getId());
                            cityBean.setName(city);
                            cityBean.setId(CitySelectorView.mProvinceListData.get(i).getList().get(j).getId());
                            cityID = cityBean.getId();
                            addSiteRulePresenter.loadShareSiteRule(AddRuleActivity.this, CitySelectorView.mProvinceListData.get(i).getList().get(j).getId(), etRuleCreator.getText().toString());
                            break;
                        }
                    }
                }
            }
        } else {
            llNoData.setVisibility(View.VISIBLE);
            lvShareSite.setVisibility(View.GONE);
            tvTips.setText("定位失败,请手动选择城市!");
        }
    }
}
